<template>
	<view class="app">
		<!-- 页面内容开始 -->

		这里是页面内容
		<button @click="test1(10)">测试并发时参数是否会混乱</button>
		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var vk = uni.vk;			// vk依赖
	export default {
		data() {
			// 页面数据变量
			return {
				// init请求返回的数据
				data:{

				},
				// 表单请求数据
				form1:{

				},
				scrollTop:0,
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		async onLoad(options = {}) {
			let that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
			let service = {
				getInfo:async function(data){
					await vk.pubfn.sleep(Math.floor(Math.random()*10) * 100);
					let userInfo = this.getLoginUser();
					return {data,userInfo};
				}
			}

			// 无效,并发有问题
			// let main = async function(data){
			// 	service.getLoginUser = function(){
			// 		return data.userInfo;
			// 	}
			// 	return await service[data.action](data.data);
			// }

			// 有效
			let main = async function(data){
				let newService = vk.pubfn.objectAssign({}, service);
				newService.getLoginUser = function(){
					return data.userInfo;
				}
				console.log("newService",newService,"oldService",service);
				return await newService[data.action](data.data);
			}

			// 有效
			// let main = async function(data){
			// 	let newService = vk.pubfn.deepClone(service);
			// 	newService.getLoginUser = function(){
			// 		return data.userInfo;
			// 	}
			// 	console.log("newService",newService,"oldService",service);
			// 	return await newService[data.action](data.data);
			// }


			let res1 = main({
				action:"getInfo",
				userInfo:{a:1},
				data:{
					a:1
				}
			}).then((res)=>{
				console.log("userInfo1",res.userInfo, "data1", res.data);
			});
			let res2 = main({
				action:"getInfo",
				userInfo:{a:2},
				data:{
					a:2
				}
			}).then((res)=>{
				console.log("userInfo2",res.userInfo, "data2", res.data);
			});
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 监听 - 点击右上角转发时 https://uniapp.dcloud.io/api/plugins/share.html#onshareappmessage
		onShareAppMessage(options) {

		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options){
				console.log("init: ",options);

			},
			test1(num){
				let that = this;
				let tokens = [
					"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiIwMDEiLCJyb2xlIjpbImFkbWluIl0sInBlcm1pc3Npb24iOltdLCJpYXQiOjE2NDgyNjQwMTUsImV4cCI6MTY0ODg2ODgxNX0.hsXPHWDUXq3KD_Stm8Ed7qxO8MLavvUMi_D1ykWDSro",
					"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiIwMDIiLCJyb2xlIjpbImFkbWluIl0sInBlcm1pc3Npb24iOltdLCJpYXQiOjE2NDgyNjM4NjcsImV4cCI6MTY0ODg2ODY2N30.G7Jc0ljT9YLps2i6oYH_g_7P8KmdiQdZwTU33vOo3DM"
				];
				for(let i=0; i<num; i++){
					let index = i % 2 == 0 ? 1:2;
					uniCloud.callFunction({
						name:"router",
						data:{
							uni_id_token:tokens[index-1],
							$url: 'client/address.test',
							data:{
								a:index
							},
						},
						success:function(data){
							if (index,data.result.userInfo._id !== "00"+index) {
								// 参数混乱了
								console.error(i+1,"data"+index,data.result.userInfo._id,"num",data.result.num, "result",data.result);
							} else {
								// 参数正常
								console.log(i+1,"data"+index,data.result.userInfo._id,"num",data.result.num, "result",data.result);
							}
						}
					});
				}
			}
		},
		// 监听器
		watch:{

		},
		// 计算属性
		computed:{

		}
	}
</script>
<style lang="scss" scoped>

</style>
